//
//  OthersHomePageViewController.m
//  TheRoadAD
//
//  Created by 宁宁 on 16/8/18.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "OthersHomePageViewController.h"

#import "TunerTableViewCell.h"                 //活动/改装 cell
#import "OthersAllVideoTableViewCell.h"        //视频cell
#import "OthersPersonalProfileTableViewCell.h" //个人 cell

#import "TrackDayDetailViewController.h"     //活动详情Controller
#import "CarAndBeautyDetailViewController.h" //查看照片Controller


@interface OthersHomePageViewController () <UIScrollViewDelegate, UITableViewDelegate, UITableViewDataSource>

@end

@implementation OthersHomePageViewController
{
    NSMutableArray *_allArray;
    NSMutableArray *_eventArray;
    NSMutableArray *_tunerArray;
    NSMutableArray *_personalArray;
    
    UIScrollView *_scrollView;
    
    /**全部 TableView*/
    UITableView *_allTableView;
    /**活动 TableView*/
    UITableView *_eventTableView;
    /**改装 TableView*/
    UITableView *_tunerTableView;
    /**个人 TableView*/
    UITableView *_personalTableView;
    
    UIView *_line;
    UIView *_statusView;
}

 static NSString *const TunerTableViewCellID = @"TunerTableViewCell";
 static NSString *const OthersPersonalProfileTableViewCellID = @"OthersPersonalProfileTableViewCell";
 static NSString *const OthersAllVideoTableViewCellID = @"OthersAllVideoTableViewCell";

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    //设置导航栏
    [self setNav];
    
    //设置区头和ScrollView
    [self setHeaderViewAndScrollView];
}

- (void)setNav{
    _allArray = [NSMutableArray array];
    _eventArray = [NSMutableArray array];
    _tunerArray = [NSMutableArray array];
    _personalArray = [NSMutableArray array];
    
    [_personalArray addObjectsFromArray:@[@"30", @"个人", @"a1234567890", @"下沙", @"本科", @"奥迪Q7", @[@"fang0.jpeg", @"fang1.jpeg", @"fang2.jpeg", @"fang3.jpeg", @"fang4.jpeg", @"fang5.jpeg", @"fang6.jpeg", @"fang7.jpeg", @"fang8.jpeg",]]];
}

- (void)setHeaderViewAndScrollView{
    
    
    
    __weak __typeof__ (self) weakSelf = self;
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Height)];
    _scrollView.delegate = self;
    _scrollView.contentSize = CGSizeMake(Screen_Width * 4, Screen_Height);
    _scrollView.pagingEnabled = YES;//按页滚动
    _scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    _allTableView = [self createTableViewAtIndex:0];
    
    _eventTableView = [self createTableViewAtIndex:1];
    
    _tunerTableView = [self createTableViewAtIndex:2];
    
    _personalTableView = [self createTableViewAtIndex:3];
    [_personalTableView registerNib:[UINib nibWithNibName:@"OthersPersonalProfileTableViewCell" bundle:nil] forCellReuseIdentifier:OthersPersonalProfileTableViewCellID];

    
    
    _headerView = [[[NSBundle mainBundle] loadNibNamed:@"OthersHomePageHeaderView" owner:nil options:nil] lastObject];
    _headerView.frame = CGRectMake(0, 0, Screen_Width, Screen_Width / 75 * 49 + 32);
    _headerView.sex.image = [UIImage imageNamed:@"fm"];
    _line = [[UIView alloc] initWithFrame:CGRectMake(Screen_Width / 2 - 140, Screen_Width / 75 * 49 + 30, 70, 2)];
    UIView *linee = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 50, 2)];
    linee.backgroundColor = UIColorFromRGB(0x03DACE);
    [_line addSubview:linee];
    [_headerView addSubview:_line];
    
    _headerView.headerViewClick = ^(int temp){
        switch (temp) {
            case 0:
                [weakSelf.navigationController popViewControllerAnimated:YES];
                break;
            case 1:
                NSLog(@"关注");
                break;
            case 2:
                [weakSelf lineScroll:0];
                break;
            case 3:
                [weakSelf lineScroll:1];
                break;
            case 4:
                [weakSelf lineScroll:2];
                break;
            case 5:
                [weakSelf lineScroll:3];
                break;
            default:
                break;
        }
    };
    [self.view addSubview:_headerView];
    
    
    _statusView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 20)];
    _statusView.backgroundColor = [UIColor clearColor];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backToTop:)];
//    tap.numberOfTapsRequired = 2;
//    [_statusView addGestureRecognizer:tap];
    [self.view addSubview:_statusView];
}

//创建tablView
- (UITableView *)createTableViewAtIndex:(NSInteger)index{
    
    UIView *HeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, Screen_Width / 75 * 49 + 32)];
    HeaderView.backgroundColor = [UIColor clearColor];
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(Screen_Width * index, 0, Screen_Width, Screen_Height)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.tableHeaderView = HeaderView;
    tableView.tag = 2000 + index;
    [tableView registerNib:[UINib nibWithNibName:@"TunerTableViewCell" bundle:nil] forCellReuseIdentifier:TunerTableViewCellID];
    [tableView registerNib:[UINib nibWithNibName:@"OthersAllVideoTableViewCell" bundle:nil] forCellReuseIdentifier:OthersAllVideoTableViewCellID];
    [_scrollView addSubview:tableView];
    return tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (tableView == _allTableView) {
        return 20;
    }else if (tableView == _eventTableView){
        return 10;
    }else if (tableView == _tunerTableView){
        return 10;
    }else return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (tableView == _allTableView) {
        int temp = indexPath.row % 3;
        if (temp == 0) {
            return 140 + Screen_Width / 25 * 12;
        }else return 100 + Screen_Width / 25 * 12;
    }
    else if (tableView == _eventTableView){
        //有内容140 没内容100
        return 140 + Screen_Width / 25 * 12;
    }
    else if (tableView == _tunerTableView){
        //没内容100
        return 100 + Screen_Width / 25 * 12;
    }
    else {
        if (Screen_Height < 600) {
            return 600;
        }else return Screen_Height;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    //全部
    if (tableView == _allTableView) {
        int tempp = indexPath.row % 3;
        //活动
        if (tempp == 0) {
            TunerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TunerTableViewCellID];
            cell.name.text = @"用户名";
            cell.time.text = @"2016-05-20 16:55";
            cell.picNum.text = @"9图";
            cell.content.text = @"21号下午黄龙约起!";
            cell.spacing.constant = 40;
            
            [cell.firstBtn setTitle:@"报名" forState:UIControlStateNormal];
            [cell.firstBtn setImage:[UIImage imageNamed:@"icon_baoming"] forState:UIControlStateNormal];
            
            cell.cellClick = ^(int temp){
                switch (temp) {
                    case 0:
                    {
                        NSLog(@"报名");
                    }
                        break;
                    case 1:
                    {
                        NSLog(@"评论");
                    }
                        break;
                    case 2:
                    {
                        NSLog(@"分享");
                    }
                        break;
                    case 3:
                    {
                        OthersHomePageViewController *others = [[OthersHomePageViewController alloc] init];
                        others.hidesBottomBarWhenPushed = YES;
                        [self.navigationController pushViewController:others animated:YES];
                    }
                    default:
                        break;
                }
            };
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        //改装
        else if (tempp == 1){
            TunerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TunerTableViewCellID];
            //    cell.headImg.image = [UIImage imageNamed:];
            //    cell.name.text =
            cell.pic.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_tu_%ld", indexPath.row % 3 + 1]];
            //    cell.picNum.text = [NSString stringWithFormat:@"%@图", ];
            cell.content.text = @"";
            cell.spacing.constant = 0;
            
            [cell.firstBtn setTitle:@"我想改" forState:UIControlStateNormal];
            [cell.firstBtn setImage:[UIImage imageNamed:@"icon_gaizhuang"] forState:UIControlStateNormal];
            
            cell.cellClick = ^(int temp){
                if (temp == 0) {
                    NSLog(@"我想改");
                }else if (temp == 1){
                    NSLog(@"评论");
                }else if (temp == 2){
                    NSLog(@"分享");
                }else{
                    
                }
            };
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
        //视频
        else{
            OthersAllVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OthersAllVideoTableViewCellID];
            cell.cellClick = ^(int temp){
                if (temp == 0) {
                    NSLog(@"赞");
                }else if (temp == 1){
                    NSLog(@"分享");
                }else {
                    NSLog(@"播放");
                }
            };
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            return cell;
        }
    }
    //活动
    else if (tableView == _eventTableView){
        TunerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TunerTableViewCellID];
        cell.name.text = @"用户名";
        cell.time.text = @"2016-05-20 16:55";
        cell.picNum.text = @"9图";
        cell.content.text = @"21号下午黄龙约起!";
        
        [cell.firstBtn setTitle:@"报名" forState:UIControlStateNormal];
        [cell.firstBtn setImage:[UIImage imageNamed:@"icon_baoming"] forState:UIControlStateNormal];
        
        cell.cellClick = ^(int temp){
            switch (temp) {
                case 0:
                {
                    NSLog(@"报名");
                }
                    break;
                case 1:
                {
                    NSLog(@"评论");
                }
                    break;
                case 2:
                {
                    NSLog(@"分享");
                }
                    break;
                case 3:
                {
                    OthersHomePageViewController *others = [[OthersHomePageViewController alloc] init];
                    others.hidesBottomBarWhenPushed = YES;
                    [self.navigationController pushViewController:others animated:YES];
                }
                default:
                    break;
            }
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;

        
        
    }
    //改装
    else if (tableView == _tunerTableView){
        TunerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TunerTableViewCellID];
        //    cell.headImg.image = [UIImage imageNamed:];
        //    cell.name.text =
        cell.pic.image = [UIImage imageNamed:[NSString stringWithFormat:@"icon_tu_%ld", indexPath.row % 3 + 1]];
        //    cell.picNum.text = [NSString stringWithFormat:@"%@图", ];
        cell.spacing.constant = 0;
        cell.cellClick = ^(int temp){
            if (temp == 0) {
                NSLog(@"我想改");
            }else if (temp == 1){
                NSLog(@"评论");
            }else if (temp == 2){
                NSLog(@"分享");
            }else{
                
            }
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    //个人
    else {
        OthersPersonalProfileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:OthersPersonalProfileTableViewCellID];
        if (_personalArray) {
            [cell setCellValue:_personalArray];
            cell.showPhoto = ^(int temp){
                CarAndBeautyDetailViewController *car = [[CarAndBeautyDetailViewController alloc] init];
                car.picArray = _personalArray[6];
                car.index = temp;
//                [car.pic setImage:[UIImage imageNamed:_personalArray[6][temp]]];
                [car.view bringSubviewToFront:car.hiddenView];
                car.bottom.constant = 100;
                car.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:car animated:YES];
            };
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TrackDayDetailViewController *detail = [[TrackDayDetailViewController alloc] init];
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
}




- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView == _scrollView) {
        CGFloat lineY = _line.frame.origin.y;
        CGFloat lineX = (Screen_Width - 280) / 2 + scrollView.contentOffset.x * (70 / Screen_Width);
        _line.frame = CGRectMake(lineX , lineY, 70, 2);
        
        if (scrollView.contentOffset.x > Screen_Width * 3) {
            _line.frame = CGRectMake(Screen_Width / 2 + 70, lineY, 90, 2);
        }else if (scrollView.contentOffset.x < 0){
            _line.frame = CGRectMake(Screen_Width / 2 - 140, lineY, 90, 2);
        }
        return;
    }
    
    if (scrollView == _allTableView || scrollView == _eventTableView || scrollView == _tunerTableView || scrollView == _personalTableView) {
        if (scrollView.contentOffset.y >= Screen_Width / 75 * 49 - 21) {
            _headerView.frame = CGRectMake(0, - (Screen_Width / 75 * 49 - 20), Screen_Width, Screen_Width / 75 * 49 + 32);
            _statusView.backgroundColor = [UIColor whiteColor];
            return;
        }else {
            _headerView.frame = CGRectMake(0, - scrollView.contentOffset.y, Screen_Width, Screen_Width / 75 * 49 + 32);
            _statusView.backgroundColor = [UIColor clearColor];
        }
    } 
}


//设置tableView的偏移量
-(void)setTableViewContentOffsetWithTag:(NSInteger)tag contentOffset:(CGFloat)offset{
    CGFloat tableViewOffset = offset;
    if(offset > Screen_Width / 75 * 49 - 20){
        tableViewOffset = Screen_Width / 75 * 49 - 20;
    }
    if (tag == 2000) {
        [_eventTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_tunerTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_personalTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
    }else if(tag == 2001){
        [_allTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_tunerTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_personalTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
    }else if (tag == 2002){
        [_allTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_eventTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_personalTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
    }else{
        [_allTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_eventTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
        [_tunerTableView setContentOffset:CGPointMake(0, tableViewOffset) animated:NO];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if ([scrollView isEqual:_scrollView]) {
        return;
    }
    [self setTableViewContentOffsetWithTag:scrollView.tag contentOffset:scrollView.contentOffset.y];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if ([scrollView isEqual:_scrollView]) {
        return;
    }
    [self setTableViewContentOffsetWithTag:scrollView.tag contentOffset:scrollView.contentOffset.y];
}


- (void)lineScroll:(int)temp{
    CGFloat lineY = _line.frame.origin.y;
    CGFloat lineX = (Screen_Width - 280) / 2 + temp * 70;
    [UIView animateWithDuration:0.2 animations:^{
        _line.frame = CGRectMake(lineX, lineY, 70, 2);
    } completion:^(BOOL finished) {
        _scrollView.contentOffset = CGPointMake(Screen_Width * temp, 0);
    }];
}

//- (void)backToTop:(UITapGestureRecognizer *)tap{
//    [_allTableView setContentOffset:CGPointMake(0, 0) animated:YES];
//    [_eventTableView setContentOffset:CGPointMake(0, 0) animated:YES];
//    [_tunerTableView setContentOffset:CGPointMake(0, 0) animated:YES];
//    [_personalTableView setContentOffset:CGPointMake(0, 0) animated:YES];
//}

@end
